﻿Imports System.IO.IsolatedStorage
Imports System.IO

Namespace IO

    Public Class IsolatedFileStorageGameDirectory
        Inherits GameDirectory

        Private Property IsolatedStorageFile() As IsolatedStorageFile

        Public Sub New(Optional ByVal path As String = "")
            Me.PhysicalPath = path
            If Me.IsolatedStorageFile Is Nothing Then
                Me.IsolatedStorageFile = GetIsolatedStorageFile()
            End If
        End Sub

        Public Sub New(ByVal path As String, ByVal ifs As IsolatedStorageFile)
            Me.PhysicalPath = path
            Me.IsolatedStorageFile = ifs
        End Sub

        Public Overrides Function CreateDirectory(relativePath As String) As Boolean
            Try
                If relativePath.EndsWith("\") Or relativePath.EndsWith("/") Then relativePath = relativePath.Substring(0, relativePath.Length - 1)
                Me.IsolatedStorageFile.CreateDirectory(relativePath)
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function

        Public Overrides Function DeleteDirectory(relativePath As String, Optional recursive As Boolean = False) As Boolean
            Try
                Me.IsolatedStorageFile.DeleteDirectory(relativePath, recursive)
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function

        Public Overrides Function DeleteFile(relativePath As String) As Boolean
            Try
                Me.IsolatedStorageFile.DeleteFile(relativePath)
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function

        Public Overrides Function DirectoryExists(relativePath As String) As Boolean
            Return Me.IsolatedStorageFile.DirectoryExists(relativePath)
        End Function

        Public Overrides Function FileExists(relativePath As String) As Boolean
            Return Me.IsolatedStorageFile.FileExists(relativePath)
        End Function

        Public Overrides Function GetDirectories() As List(Of String)
#If NETFX_CORE Then
            Return Me.IsolatedStorageFile.GetDirectoryNames(Function(item) True).ToList()
#Else
            Return Me.IsolatedStorageFile.GetDirectoryNames().ToList()
#End If
        End Function

        Public Overloads Overrides Function GetDirectories(relativePath As String) As List(Of String)
            Return Me.IsolatedStorageFile.GetDirectoryNames(relativePath).ToList()
        End Function

        Public Overrides Function GetFiles() As List(Of String)
#If NETFX_CORE Then
            Return Me.IsolatedStorageFile.GetFileNames(Function(item) True).ToList()
#Else
            Return Me.IsolatedStorageFile.GetFileNames().ToList()
#End If
        End Function

        Public Overloads Overrides Function GetFiles(relativePath As String) As List(Of String)
            Return Me.IsolatedStorageFile.GetFileNames(Path.Combine(relativePath, "*.*")).ToList()
        End Function

        Public Overrides Function OpenFile(relativePath As String, mode As Runa.Foundation.IO.FileMode, Optional ByVal fileAccess As FileAccess = FileAccess.ReadWrite) As Stream
            Dim e As Exception = Nothing
            For i As Integer = 0 To 10
                Try
                    Return Me.IsolatedStorageFile.OpenFile(relativePath, mode, fileAccess, FileShare.Read)
                Catch ex As Exception
                    e = ex
                End Try
            Next
            Throw e
        End Function

        Public Shared Function GetIsolatedStorageFile() As IsolatedStorageFile
#If WINDOWS_PHONE Then
            Return IsolatedStorageFile.GetUserStoreForApplication()
#Else
            Return IsolatedStorageFile.GetUserStoreForAssembly()
#End If
        End Function

    End Class

End Namespace